#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD7124.h"
#include "AD7124_REG.h"
#include "Serial.h"

#define DATA_NUMBER	9
float Vref;
uint32_t data[DATA_NUMBER];
uint32_t data_result[DATA_NUMBER];
uint8_t err_code;
/*********函数声明*********/
uint8_t ad7124_channel_config(void);
uint8_t get_data_byte(uint32_t data, uint16_t target_byte);
void get_data_array(uint32_t * array);
void show_adc_data(void);
void show_uart_TxData(void);
void show_uart_TxData(void);

int main(void)
{
	/*模块初始化*/
	AD7124_Init();	
	OLED_Init();		//OLED初始化
	Serial_Init();

	
	/*AD7124复位*/
	AD7124_Reset();
	/*OLED显示*/
//	OLED_ShowString( 1, 2, "AD7124 Drive");
	
	/******配置AD7124寄存器******/
	
	/*控制寄存器设置*/
	AD7124_ADC_CTRL_Set( 0x03, 0xC0);
	/*配置寄存器设置，更改量程*/
	Vref = AD7124_Config_Set( REG_CONFIG_0, AD7124_CONFIG_POLARITY_UNIPOLAR,
						AD7124_CONFIG_Burnout_OFF, AD7124_CONFIG_REF_BUFP_DISABLE,
						AD7124_CONFIG_REF_BUFM_DISABLE, AD7124_CONFIG_AIN_BUFP_DISABLE,
						AD7124_CONFIG_AIN_BUFM_DISABLE, AD7124_CONFIG_REF_SEL_InternalRefVoltage,
						AD7124_CONFIG_PGA_2500);
	/* 配置通道 */
	err_code = ad7124_channel_config();
	if( err_code)
		OLED_ShowHexNum( 2, 2, err_code, 2);

	uint8_t channel_x = 0;
	while (1)
	{
		/*串口接收处理*/
		if(Serial_GetRxFlag() == 1)
		{
			
		}
		/* 读取ADC数据 */
		AD7124_EN(1);
		if( AD7124_R_RDY() == 0)
		{
			
			AD7124_R_REG( REG_DATA);
			data[channel_x] = AD7124_ReadData( 3);
			data_result[channel_x] = (uint32_t)(((float)data[channel_x] / 0xFFFFFF) * Vref * 1000) ;
			channel_x ++;
			if( channel_x >= DATA_NUMBER)
			{	
				channel_x = 0;	
				get_data_array(data_result);
				Serial_SendPacket( Serial_Tx_Data);
				/* 显示要发送的数据 */
				show_uart_TxData();
			}
		}
		AD7124_EN(0);
		/* 显示ADC数据 */
		show_adc_data();
	}
}

/**
  * @brief	AD7124通道配置
  * @param none
  * @retval	错误信息
  */
uint8_t ad7124_channel_config(void)
{
	//channel0 -> AIN0
	err_code = AD7124_Channel_Set( REG_CHANNEL_0, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN0, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 1;
	//channel1 -> AIN2
	err_code = AD7124_Channel_Set( REG_CHANNEL_1, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN2, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 2;
	//channel2 -> AIN4
	err_code = AD7124_Channel_Set( REG_CHANNEL_2, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN4, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 3;
	//channel3 -> AIN6
	err_code = AD7124_Channel_Set( REG_CHANNEL_3, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN6, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 4;
	//channel4 -> AIN8
	err_code = AD7124_Channel_Set( REG_CHANNEL_4, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN8, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 5;
	//channel5 -> AIN10
	err_code = AD7124_Channel_Set( REG_CHANNEL_5, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN10, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 6;
	//channel6 -> AIN12
	err_code = AD7124_Channel_Set( REG_CHANNEL_6, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN12, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 7;
	//channel7 -> AIN14
	err_code = AD7124_Channel_Set( REG_CHANNEL_7, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN14, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 8;
	//channel8 -> AIN15
	err_code = AD7124_Channel_Set( REG_CHANNEL_8, AD7124_CHANNEL_ENABLE, AD7124_CHANNEL_CONFIG_0, 
									AD7124_CHANNEL_AINP_AIN15, AD7124_CHANNEL_AINM_AVSS);
	if( err_code == 0 )	
		return 9;
	return 0;
}
	

/**
  * @brief	取出float数据中指定的八位
  * @param  data:需要操作的数据
  * @param  target_byte:指定的位数
  *         1:高八位
  *			2:次高八位
  *			3:次低八位
  *			4:低八位
  *	@retval	取出的8位数
  */
uint8_t get_data_byte(uint32_t data, uint16_t target_byte)
{
	if( target_byte == 1)
	{
		return (data >> 24) & 0xFF;
	}
	else if(target_byte == 2)
	{
		return (data >> 16) & 0xFF;
	}
	else if(target_byte == 3)
	{
		return (data >> 8) & 0xFF;
	}
	else if(target_byte == 4)
	{
		return data & 0xFF;
	}
	return 0xFF;
}
/**
  * @brief	将数据分字节放入串口发送数组中
  * @param  array:存放数据的数组指针
  */
void get_data_array(uint32_t * array)
{
	for( int i = 0; i < DATA_NUMBER; i ++)
	{
		for(int j = 1 ; j < 5; j ++)
		{
			Serial_Tx_Data[i * 4 + j - 1] = get_data_byte(array[i], j);
		}
	}
	
}

/**
  * @brief	ADC数据显示
  * @param  None
  */
void show_adc_data(void)
{
	OLED_ShowNum( 1, 1, data_result[0], 7); 
	OLED_ShowNum( 1, 9, data_result[1], 7);
	OLED_ShowNum( 2, 1, data_result[2], 7);
	OLED_ShowNum( 2, 9, data_result[3], 7); 
	OLED_ShowNum( 3, 1, data_result[4], 7);
	OLED_ShowNum( 3, 9, data_result[5], 7);
	OLED_ShowNum( 4, 1, data_result[6], 7); 
	OLED_ShowNum( 4, 9, data_result[7], 7);
	OLED_ShowNum( 4, 9, data_result[8], 7);
}

/**
  * @brief	UART发送数据显示
  * @param  None
  */
void show_uart_TxData(void)
{
	OLED_ShowHexNum( 1, 1, Serial_Tx_Data[0], 2); 
	OLED_ShowHexNum( 1, 5, Serial_Tx_Data[1], 2);
	OLED_ShowHexNum( 1, 9, Serial_Tx_Data[2], 2);
	OLED_ShowHexNum( 2, 1, Serial_Tx_Data[3], 2); 
	OLED_ShowHexNum( 2, 5, Serial_Tx_Data[4], 2);
	OLED_ShowHexNum( 2, 9, Serial_Tx_Data[5], 2);
	OLED_ShowHexNum( 3, 1, Serial_Tx_Data[6], 2); 
	OLED_ShowHexNum( 3, 5, Serial_Tx_Data[7], 2);
	OLED_ShowHexNum( 3, 9, Serial_Tx_Data[8], 2);
}



